package com.ljkj.cordial.chat.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ljkj.cordial.chat.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class RectButtonHintDialog extends Dialog {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.tv_left_submit)
    TextView tvLeftSubmit;
    @BindView(R.id.tv_right_submit)
    TextView tvRightSubmit;
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;
    @BindView(R.id.v_mid)
    View vMid;
    private Unbinder unbinder;

    public RectButtonHintDialog(@NonNull Context context) {
        super(context, com.nic.view.R.style.dialog_no_bg);
        setContentView(R.layout.dialog_common_rect_hint);
        unbinder = ButterKnife.bind(this);
        setCancelable(false);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        int dp8 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, getContext().getResources().getDisplayMetrics());
        getWindow().getDecorView().setPadding(dp8, 0, dp8, 0);
    }

    /**
     * 显示于前台
     *
     * @return
     */
//    @RequiresApi(api = Build.VERSION_CODES.M)
//    public RectButtonHintDialog alertWindow() {
//        if (!Settings.canDrawOverlays(getContext())) {
//            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
//            getContext().startActivity(intent);
//            return this;
//        }
//        getWindow().setType(
//                (WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG));
//        return this;
//    }
    @Override
    public void dismiss() {
        super.dismiss();
        unbinder.unbind();
    }

    public RectButtonHintDialog setTitle(String title) {
        tvTitle.setText(title);
        return this;
    }

    public RectButtonHintDialog setContent(String content) {
        tvContent.setVisibility(View.VISIBLE);
        tvContent.setText(content);
        return this;
    }

    public RectButtonHintDialog setSubmit(String content, final View.OnClickListener listener) {
        return setSubmit(content, listener, true);
    }

    public RectButtonHintDialog setSubmit(String content, final View.OnClickListener listener, boolean isDismiss) {
        tvLeftSubmit.setVisibility(View.GONE);
        vMid.setVisibility(View.GONE);
        tvRightSubmit.setText(content);
        tvRightSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isDismiss)
                    dismiss();
                if (listener != null) {
                    listener.onClick(v);
                }
            }
        });

        return this;
    }

    public RectButtonHintDialog setLeftSubmit(String content, final View.OnClickListener listener) {
        tvLeftSubmit.setText(content);
        tvLeftSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (listener != null) {
                    listener.onClick(v);
                }
            }
        });

        return this;
    }

    public RectButtonHintDialog setRightSubmit(String content, final View.OnClickListener listener) {
        return setRightSubmit(content, listener, true);
    }

    public RectButtonHintDialog setRightSubmit(String content, final View.OnClickListener listener, boolean isDismiss) {
        tvRightSubmit.setText(content);
        tvRightSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isDismiss)
                    dismiss();
                if (listener != null) {
                    listener.onClick(v);
                }
            }
        });
        return this;
    }
}
